﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ElectrosLTD.Models;


namespace ElectrosLTD.Controllers
{
    public class UserController : Controller
    {
        //
        // GET: /User/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Register()
        {
            return View(new RegistrationModel());
        }

        [HttpPost]
        public ActionResult Register(RegistrationModel rm)
        {
            {
                Common.User user = new Common.User();

                user.FirstName = rm.Name;
                user.LastName = rm.Surname;
                user.Email = rm.Email;
                user.HouseNo = rm.HouseNo;
                user.Street = rm.Street;
                user.Postcode = rm.Postcode;
                user.Town = rm.Town;
                user.Mobile = Convert.ToInt32(rm.Mobile);
                user.CountryID = rm.countryID;
                user.Username = rm.username;
                user.Password = rm.password;

                new UserServ.UserWCFClient().Register(user);
                Common.Type t = new UserServ.UserWCFClient().GetRole(5);
                new UserServ.UserWCFClient().AllocateRole(user, t);

                return RedirectToAction("Index", "Home");
            }
        }

        public JsonResult DoesEmailExist(string email)
        {
            bool doesExist = new UserServ.UserWCFClient().DoesEmailExist(email);

            if (!doesExist)
            {
                return Json(true, JsonRequestBehavior.AllowGet);
            }

            return Json(false, JsonRequestBehavior.AllowGet);

        }

       public JsonResult DoesUsernameExist(string username)
        {
            bool doesExist = new UserServ.UserWCFClient().DoesUsernameExist(username);

            if (!doesExist)
            {
                return Json(true, JsonRequestBehavior.AllowGet);
            }

            return Json(false, JsonRequestBehavior.AllowGet);

        }



    }
}
